package com.naizhuai.ui.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.TaskActionApiStore;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.adapter.GridSpacingItemDecoration;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.StepJoin;
import com.naizhuai.ui.entity.mainbusiness.ActionBaseBean;
import com.naizhuai.ui.task.adapter.ActionAdapter;
import com.naizhuai.ui.task.adapter.TaskExplainAdapter;
import com.naizhuai.util.ViewUtils;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

import static com.naizhuai.ui.task.ActionSetting1Activity.PAGESTATE;
import static com.naizhuai.ui.task.ActionSetting1Activity.NOCORRELATE;
import static com.naizhuai.ui.task.ActionSetting1Activity.CORRELATE;
import static com.naizhuai.ui.entity.mainbusiness.ActionBaseBean.TASKFLOWID;

public class StepJoinActivity extends BaseActivity implements ActionAdapter.CallBackQuestion {
    private static String STEP = "step";
    @BindView(R.id.toolbar)
    Toolbar settingToolbar;
    @BindView(R.id.stepname)
    TextView stepname;
    @BindView(R.id.avatar)
    CircleImageView avatar;
    @BindView(R.id.imgTextinfo)
    ImageView imgTextinfo;
    @BindView(R.id.textlist)
    RecyclerView textlist;
    @BindView(R.id.steptitle)
    TextView steptitle;
    @BindView(R.id.flowRecycleList)
    RecyclerView flowRecycleList;
    @BindView(R.id.action_count)
    TextView actionCount;
    @BindView(R.id.imageTextContainer)
    ConstraintLayout imageTextContainer;
    @BindView(R.id.fab_add_task)
    FloatingActionButton fabAddTask;
    @BindView(R.id.number)
    TextView number;

    public static void start(Context context, String taskFlowId, String step) {
        Intent intent = new Intent(context, StepJoinActivity.class);
        intent.putExtra(TASKFLOWID, taskFlowId);
        intent.putExtra(STEP, step);
        context.startActivity(intent);
    }

    private TaskActionApiStore mTaskActionApiStore;
    private TaskExplainAdapter mTaskExplainAdapter;
    private ActionAdapter mActionAdapter;
    private String taskFlowId;
    private String step;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitView();
        InitData();
    }

    private void InitData() {
        taskFlowId = getStrExtra(TASKFLOWID);
        step = getStrExtra(STEP);
        number.setText(step);
        if (mTaskActionApiStore == null) {
            mTaskActionApiStore = TaskActionApiIml.getInstance();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadJoinData();
    }

    private void loadJoinData() {
        showWaitDiaLog();
        mTaskActionApiStore.task_flow_joined(taskFlowId)
                .subscribe(new NetWorkObserver<StepJoin>() {
                    @Override
                    protected void onFail(String errormsg) {
                        hideWaitDialog();
                        shortToast(errormsg);
                    }

                    @Override
                    protected void onSuccess(StepJoin join) {
                        hideWaitDialog();
                        stepname.setText(join.stepName);
                        ViewUtils.showImg(join.managerUser.thumb, avatar);
                        ViewUtils.showImg(join.imageUrl, imgTextinfo);
                        mTaskExplainAdapter.setStringList(join.detail);
                        int size = join.questionList.size();
                        if (size > 0) {
                            actionCount.setText(String.format(getString(R.string.flow_count), size + ""));
                            mActionAdapter.setActionData(join.questionList);
                        } else {
                            //暂无可查看的行为
                        }
                    }
                });
    }

    private void InitView() {
        settingToolbar.setNavigationOnClickListener(v -> finish());
        //设置文字说明
        mTaskExplainAdapter = new TaskExplainAdapter();
        mTaskExplainAdapter.setEditEnable(false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        textlist.setLayoutManager(layoutManager);
        textlist.setAdapter(mTaskExplainAdapter);

        //设置Action页面
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        flowRecycleList.setLayoutManager(gridLayoutManager);
        flowRecycleList.addItemDecoration(new GridSpacingItemDecoration(2,
                getResources().getDimensionPixelSize(R.dimen.line_space), false));
        mActionAdapter = new ActionAdapter(this);
        flowRecycleList.setAdapter(mActionAdapter);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_step_join;
    }

    @OnClick(R.id.fab_add_task)
    public void onAddClicked() {
        Intent intent = new Intent(this, ActionSetting1Activity.class);
        intent.putExtra(PAGESTATE,NOCORRELATE);
        intent.putExtra(TASKFLOWID,taskFlowId);
        startActivity(intent);
    }

    @Override
    public void onClickAction(String questionid) {
        Intent intent=new Intent(this,ActionSetting1Activity.class);
        intent.putExtra(PAGESTATE,CORRELATE);
        intent.putExtra(ActionBaseBean.QUESTION_ID_KEY,questionid);
        startActivity(intent);
    }
}
